package ift605.EquationWithTrainingAgents.behavior.common;

import ift605.EquationWithTrainingAgents.agent.PoolAgent;
import ift605.EquationWithTrainingAgents.agent.TrainingAgentClient;
import ift605.EquationWithTrainingAgents.struct.LearningMessage;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

public class VerificationBehavior extends CyclicBehaviour {
	private static final long serialVersionUID = 1L;

	@Override
	public void action() {
		try
		{
			ACLMessage request = null;
			MessageTemplate mt = MessageTemplate.MatchConversationId(LearningMessage.VERIFY_ANSWER);
			request = myAgent.receive(mt);
			
			if(request != null)
			{
				if(((PoolAgent) myAgent).isValidPerformative(request.getPerformative())){
					try {
						LearningMessage requestMess = (LearningMessage)request.getContentObject(); 
						
						if(requestMess.isResolved){
							//System.out.println(myAgent.getLocalName() + " send final answers");
							
							// send the ASK_TO_REGISTER to LearningDerivativeAgent for registration procedure
							ACLMessage response = new ACLMessage(requestMess.requestID);
							response.addReceiver(new AID(TrainingAgentClient.NEW_AID_NAME_DERIVATIVE,  AID.ISLOCALNAME));
							response.setConversationId(LearningMessage.ASK_TO_REGISTER);
							//requestMess.derived.printUserReadable();
							response.setContentObject(requestMess);
							myAgent.send(response);
							
							// Send the FINAL_ANSWER to LearningDerivativeAgent
							//requestMess.derived.printUserReadable();
							response.setConversationId(LearningMessage.FINAL_ANSWER);
							myAgent.send(response);
						}
						else{
							// Continue propagation
							if(requestMess.tryDerive){

								ACLMessage response = new ACLMessage(requestMess.requestID);
								response.setConversationId(LearningMessage.DERIVE);
								response.setContentObject(requestMess);
								
								PoolAgent.AddAIDFromDF(PoolAgent.POOL_AGENT_DF, response, myAgent);
								// Send
								myAgent.send(response);
							}
							else{
								//System.out.println(myAgent.getLocalName() + " confirm that derivation process didn't work");
								
								// Send to Learning Agent that the derivative process didn't work
								ACLMessage response = new ACLMessage(requestMess.requestID);
								response.addReceiver(new AID(TrainingAgentClient.NEW_AID_NAME_DERIVATIVE, AID.ISLOCALNAME));
								response.setConversationId(LearningMessage.FINAL_ANSWER);
								requestMess.derived = null;
								response.setContentObject(requestMess);
								myAgent.send(response);
							}
						}
					} catch (UnreadableException e) {
						e.printStackTrace();
					}	
				}
			}
			else
			{
				block();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
